<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog" style="width: 1000px; margin: 0 auto;">
  <el-form ref="addDept" label-width="100px" :model="formData" :rules="rules" class="form">
    <el-form-item label="部门名称"   prop="name">
        <el-input style="width: 80%;" size="mini" placeholder="2-10个字符" v-model="formData.name"></el-input>
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
        <el-input style="width: 80%;" size="mini" placeholder="2-10个字符" v-model="formData.code"></el-input>
    </el-form-item>
    <el-form-item label="部门负责人" prop="managerId">
        <el-select style="width: 80%;" size="mini" v-model="formData.managerId" placeholder="请选择部门负责人">
            <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id"></el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
        <el-input style="width: 80%;"  type="textarea" placeholder="2-10个字符" :rows="4" v-model="formData.introduce"></el-input>
    </el-form-item>
    <el-form-item >
       <el-row class="center">
        <el-col :span="12">
            <el-button mini="size" type="primary" @click="confirmBtn">确定</el-button>
            <el-button mini="size" @click="cancelBtn">取消</el-button>
        </el-col>
       </el-row>
    </el-form-item>
  </el-form>

  </el-dialog>
</template>

<script>
import { updateDepartment,getDepartmentList ,getDepManageList, addDepartment, getDepartmentDetail} from '@/api/department'
export default {
    props: {
        showDialog: {
            type: Boolean,
            default: false
        },
        currentNodeId: {
            type: Number,
            default: null
        }
    },
    methods: {
        // 关闭弹层
        closeDialog() {
            // 清空1表单
            // resetFields() 只能重置在模板中绑定的数据, 未在模板中使用的数据将不会被置空
            //个别数据需要手动置空
            this.formData = {}
            this.$refs.addDept.resetFields()
            
            this.$emit('update:showDialog', false)
        },
        async getManageData() {
            const res = await getDepManageList()
            console.log(res);
            this.managerList = res
        },
        // 点击确认
       confirmBtn (){
            // 表单校验
            this.$refs.addDept.validate(async (valid) => {
                // 判断校验是否通过
                if (valid) {
                    let message =  '新增'
                    // 新增部门
                    if (!this.formData.id) {
                        const res = await addDepartment({...this.formData, pid: this.currentNodeId})
                        console.log(res);
                        // 通知父组件更新数据
                    }
                    // 编辑部门
                    else {
                        const res = await updateDepartment(this.formData)
                        message = '编辑'
                    }

                    this.$emit('updateData')
                        // 提示用户
                        this.$message.success(`${message}部门成功`)
                        // 清空表单
                        this.$refs.addDept.resetFields()
                        // 关闭弹层
                        this.closeDialog()
                 
                }

            })

            console.log('dasda');


        },
        // 点击取消
        cancelBtn() {
            // 清空1表单
            this.$refs.addDept.resetFields()
            // 关闭弹层
            this.closeDialog()
        },

        // 获取组织详情
        async getDepartmentDetail() {
        //    alert('casmpcsoac')
        const res = await getDepartmentDetail(this.currentNodeId)
        // console.log(res);
        this.formData = res
        }
        
    },
    data() {
        return {
           formData: {
            code: '', // 部门编码
            introduce: '', // 部门介绍
            managerId: '', // 部门负责人id
            name: '', // 部门名称
            pid: '' // 父级部门的id
           },
           rules: {
            name: [
                {
                    required: true,
                    message: '部门名称不能为空',
                    trigger: 'blur'
                },
                {
                    min: 2,
                    max: 10,
                    message: '部门名称要求2-10个字符',
                    trigger: 'blur'
                },
                {
                    validator: async (rule, value, callback) => {
                        let res = await getDepartmentList()
                        if (this.formData.id) {
                            res = res.filter(item => item.id !== this.formData.id)
                        }
                       
                        if (res.some(item => item.name === value)) {
                            callback(new Error('部门名称已存在'))
                        }
                        else {
                            callback()
                        }
                    },
                    trigger: 'blur'
                }
            ],
            code: [
                {
                    required: true,
                    message: '部门编码不能为空',
                    trigger: 'blur'
                },
                {
                    min: 2,
                    max: 10,
                    message: '部门编码要求2-10个字符',
                    trigger: 'blur'
                },
                {
                    validator: async (rule, value, callback) => {
                        let res = await getDepartmentList()
                        if (this.formData.id) {
                            res = res.filter(item => item.id !== this.formData.id)
                        }
                        if (res.some(item => item.code === value)) {
                            callback(new Error('部门已存在'))
                        }
                        else {
                            callback()
                        }
                    },
                    trigger: 'blur'
                }
            ],
            managerId: [
                {
                    required: true,
                    message: '部门负责人不能为空',
                    trigger: 'blur'
                }
            ],
            introduce: [
                {
                    required: true,
                    message: '部门介绍不能为空',
                    trigger: 'blur' 
                },
                {
                    min: 2,
                    max: 30,
                    message: '部门介绍要求2-30个字符',
                    trigger: 'blur'
                }
            ]
           },
           managerList: []
        }
    },
    created() {
        this.getManageData()
    },
    computed: {
        showTitle() {
            return this.formData.id ? '编辑部门' : '新增部门'
        }
    }
}
</script>

<style>
.center {
   display: flex;
   justify-content: center;
}
.form {
    display: flex;
    justify-content: center;
    flex-direction: column;
}
</style>